﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GIMS.MODEL;

namespace KPIS.GERP.GIMS.DAL
{
    public sealed class PurchaseOrderCollateralDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public PurchaseOrderCollateralDAL()
        {

        }

        public PurchaseOrderCollateralInfo GetPurchaseOrderCollateral(int seq)
        {
            string strSQL;
            DataSet ds;
            DataRowCollection dr;
            PurchaseOrderCollateralInfo info = new PurchaseOrderCollateralInfo();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@seq", seq);

                strSQL = "SELECT p.*"
                            + ", cc.ICM_COL_CAT_NAME"
                            + ", ct.ICM_COL_TYPE_NAME"
                        + " FROM ICM_PURCHASE_ORDER_COLLATERAL p"
                            + " INNER JOIN ICM_SYS_COLLATERAL_CATEGORY cc ON cc.ICM_COL_CAT_SEQ = p.ICM_COL_CAT_SEQ"
                            + " INNER JOIN ICM_SYS_COLLATERAL_TYPE ct ON ct.ICM_COL_TYPE_SEQ = p.ICM_COL_TYPE_SEQ"
                        + " WHERE p.ICM_PO_COL_SEQ = @seq";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    PurchaseOrderInfo infoPo = new PurchaseOrderInfo();
                    SysCollateralTypeInfo infoType = new SysCollateralTypeInfo();
                    SysCollateralCategoryInfo infoCategory = new SysCollateralCategoryInfo(); 
                    RecordInfo infoRecord = new RecordInfo();

                    info.Seq = Convert.ToInt32(dr[0]["ICM_PO_COL_SEQ"]);
                    infoPo.Seq = Convert.ToInt32(dr[0]["ICM_PO_SEQ"]);
                    infoCategory.Seq = (Convert.IsDBNull(dr[0]["ICM_COL_CAT_SEQ"])) ? null : (Nullable<int>)Convert.ToInt32(dr[0]["ICM_COL_CAT_SEQ"]);
                    infoCategory.Name = dr[0]["ICM_COL_CAT_NAME"].ToString();
                    infoType.Seq = (Convert.IsDBNull(dr[0]["ICM_COL_TYPE_SEQ"])) ? null : (Nullable<int>)Convert.ToInt32(dr[0]["ICM_COL_TYPE_SEQ"]);
                    infoType.Name = dr[0]["ICM_COL_TYPE_NAME"].ToString();
                    info.RefNo = dr[0]["ICM_PO_COL_REF_NO"].ToString();
                    info.ReceiptNo = dr[0]["RECEIPT_NO"].ToString();
                    info.Amount = (Convert.IsDBNull(dr[0]["ICM_PO_COL_AMOUNT"])) ? null : (Nullable<double>)Convert.ToInt32(dr[0]["ICM_PO_COL_AMOUNT"]);
                    info.Descr = dr[0]["ICM_PO_COL_DESCR"].ToString();

                    infoRecord.Created_by = (Convert.IsDBNull(dr[0]["CREATED_BY"]) ? null : (Nullable<int>)(dr[0]["CREATED_BY"]));
                    infoRecord.Created_when = (Convert.IsDBNull(dr[0]["CREATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[0]["CREATED_WHEN"]));
                    infoRecord.Updated_by = (Convert.IsDBNull(dr[0]["UPDATED_BY"]) ? null : (Nullable<int>)(dr[0]["UPDATED_BY"]));
                    infoRecord.Updated_when = (Convert.IsDBNull(dr[0]["UPDATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[0]["UPDATED_WHEN"]));

                    info.InfoPo = infoPo;
                    info.InfoCategory = infoCategory;
                    info.InfoType = infoType;
                    info.InfoRecord = infoRecord;
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        public IList<PurchaseOrderCollateralInfo> GetPurchaseOrderCollateralWithParent(int po_seq)
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<PurchaseOrderCollateralInfo> infoList = new List<PurchaseOrderCollateralInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@icm_po_seq", po_seq);

                strSQL = "SELECT p.*"
                            + ", cc.ICM_COL_CAT_NAME"
                            + ", ct.ICM_COL_TYPE_NAME"
                        + " FROM ICM_PURCHASE_ORDER_COLLATERAL p"
                            + " INNER JOIN ICM_SYS_COLLATERAL_CATEGORY cc ON cc.ICM_COL_CAT_SEQ = p.ICM_COL_CAT_SEQ"
                            + " INNER JOIN ICM_SYS_COLLATERAL_TYPE ct ON ct.ICM_COL_TYPE_SEQ = p.ICM_COL_TYPE_SEQ"
                        + " WHERE p.ICM_PO_SEQ = @icm_po_seq";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        PurchaseOrderCollateralInfo info = new PurchaseOrderCollateralInfo();
                        PurchaseOrderInfo infoPo = new PurchaseOrderInfo();
                        SysCollateralTypeInfo infoType = new SysCollateralTypeInfo();
                        SysCollateralCategoryInfo infoCategory = new SysCollateralCategoryInfo();
                        RecordInfo infoRecord = new RecordInfo();

                        info.Seq = Convert.ToInt32(dr[0]["ICM_PO_COL_SEQ"]);
                        infoPo.Seq = Convert.ToInt32(dr[0]["ICM_PO_SEQ"]);
                        infoCategory.Seq = (Convert.IsDBNull(dr[0]["ICM_COL_CAT_SEQ"])) ? null : (Nullable<int>)Convert.ToInt32(dr[0]["ICM_COL_CAT_SEQ"]);
                        infoCategory.Name = dr[0]["ICM_COL_CAT_NAME"].ToString();
                        infoType.Seq = (Convert.IsDBNull(dr[0]["ICM_COL_TYPE_SEQ"])) ? null : (Nullable<int>)Convert.ToInt32(dr[0]["ICM_COL_TYPE_SEQ"]);
                        infoType.Name = dr[0]["ICM_COL_TYPE_NAME"].ToString();
                        info.RefNo = dr[0]["ICM_PO_COL_REF_NO"].ToString();
                        info.ReceiptNo = dr[0]["RECEIPT_NO"].ToString();
                        info.Amount = (Convert.IsDBNull(dr[0]["ICM_PO_COL_AMOUNT"])) ? null : (Nullable<double>)Convert.ToInt32(dr[0]["ICM_PO_COL_AMOUNT"]);
                        info.Descr = dr[0]["ICM_PO_COL_DESCR"].ToString();

                        infoRecord.Created_by = (Convert.IsDBNull(dr[0]["CREATED_BY"]) ? null : (Nullable<int>)(dr[0]["CREATED_BY"]));
                        infoRecord.Created_when = (Convert.IsDBNull(dr[0]["CREATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[0]["CREATED_WHEN"]));
                        infoRecord.Updated_by = (Convert.IsDBNull(dr[0]["UPDATED_BY"]) ? null : (Nullable<int>)(dr[0]["UPDATED_BY"]));
                        infoRecord.Updated_when = (Convert.IsDBNull(dr[0]["UPDATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[0]["UPDATED_WHEN"]));

                        info.InfoPo = infoPo;
                        info.InfoCategory = infoCategory;
                        info.InfoType = infoType;
                        info.InfoRecord = infoRecord;

                        infoList.Add(info);
                    }
                }
                return infoList;
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Insert(IList<PurchaseOrderCollateralInfo> infoList, Nullable<int> po_seq, PurchaseOrderInfo info, IDBManager dbManagerTrans)
        {
            string strSQL;
            try
            {
                for (int i = 0; i < infoList.Count; i++)
                {
                    dbManagerTrans.CreateParameters(8);

                    dbManagerTrans.AddParameters(0, "@col_type_seq", infoList[i].InfoType.Seq);
                    dbManagerTrans.AddParameters(1, "@col_cat_seq", infoList[i].InfoCategory.Seq);
                    dbManagerTrans.AddParameters(2, "@ref_no", infoList[i].RefNo);
                    dbManagerTrans.AddParameters(3, "@amount", infoList[i].Amount);
                    dbManagerTrans.AddParameters(4, "@descr", infoList[i].Descr);
                    dbManagerTrans.AddParameters(5, "@po_seq", po_seq);
                    dbManagerTrans.AddParameters(6, "@created_by", info.InfoRecord.Created_by);
                    dbManagerTrans.AddParameters(7, "@created_when", info.InfoRecord.Created_when);

                    strSQL = "INSERT INTO ICM_PURCHASE_ORDER_COLLATERAL"
                     + " (ICM_COL_TYPE_SEQ, ICM_COL_CAT_SEQ, ICM_PO_COL_REF_NO, ICM_PO_COL_AMOUNT"
                     + " , ICM_PO_COL_DESCR, ICM_PO_SEQ"
                     + " , CREATED_BY, CREATED_WHEN)"
                     + " VALUES (@col_type_seq"
                     + ", @col_cat_seq"
                     + ", @ref_no"
                     + ", @amount"
                     + ", @descr"
                     + ", @po_seq"
                     + ", @created_by"
                     + ", @created_when"
                     + " )";

                    dbManagerTrans.ExecuteNonQuery(CommandType.Text, strSQL);
                }
            }
            catch
            {
                throw;
            }
        }

        public void Insert(PurchaseOrderCollateralInfo info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(8);

                dbManager.AddParameters(0, "@col_type_seq", info.InfoType.Seq);
                dbManager.AddParameters(1, "@col_cat_seq", info.InfoCategory.Seq);
                dbManager.AddParameters(2, "@ref_no", info.RefNo);
                dbManager.AddParameters(3, "@amount", info.Amount);
                dbManager.AddParameters(4, "@descr", info.Descr);
                dbManager.AddParameters(5, "@po_seq", info.InfoPo.Seq);
                dbManager.AddParameters(6, "@created_by", info.InfoRecord.Created_by);
                dbManager.AddParameters(7, "@created_when", info.InfoRecord.Created_when);

                strSQL = "INSERT INTO ICM_PURCHASE_ORDER_COLLATERAL"
                     + " (ICM_COL_TYPE_SEQ, ICM_COL_CAT_SEQ, ICM_PO_COL_REF_NO, ICM_PO_COL_AMOUNT"
                     + " , ICM_PO_COL_DESCR, ICM_PO_SEQ"
                     + " , CREATED_BY, CREATED_WHEN)"
                     + " VALUES (@col_type_seq"
                     + ", @col_cat_seq"
                     + ", @ref_no"
                     + ", @amount"
                     + ", @descr"
                     + ", @po_seq"
                     + ", @created_by"
                     + ", @created_when"
                     + " )";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(PurchaseOrderCollateralInfo info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(8);

                dbManager.AddParameters(0, "@seq", info.Seq);
                dbManager.AddParameters(1, "@col_type_seq", info.InfoType.Seq);
                dbManager.AddParameters(2, "@col_cat_seq", info.InfoCategory.Seq);
                dbManager.AddParameters(3, "@ref_no", info.RefNo);
                dbManager.AddParameters(4, "@amount", info.Amount);
                dbManager.AddParameters(5, "@descr", info.Descr);
                dbManager.AddParameters(6, "@updated_by", info.InfoRecord.Updated_by);
                dbManager.AddParameters(7, "@updated_when", info.InfoRecord.Updated_when);

                strSQL = "UPDATE ICM_PURCHASE_ORDER_COLLATERAL"
                 + " SET ICM_COL_TYPE_SEQ = @col_type_seq"
                 + ", ICM_COL_CAT_SEQ = @col_cat_seq"
                 + ", ICM_PO_COL_REF_NO = @ref_no"
                 + ", ICM_PO_COL_AMOUNT = @amount"
                 + ", ICM_PO_COL_DESCR = @descr"
                 + ", UPDATED_BY = @updated_by"
                 + ", UPDATED_WHEN = @updated_when"
                 + " WHERE ICM_PO_COL_SEQ = @seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void DeleteAll(IList<PurchaseOrderCollateralInfo> infoList)
        {
            string strSQL;
            string seq = "";
            for (int i = 0; i < infoList.Count; i++)
            {
                if (i == 0)
                {
                    seq += infoList[i].Seq;
                }
                else
                {
                    seq += "," + infoList[i].Seq;
                }
            }

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();

                strSQL = "DELETE FROM ICM_PURCHASE_ORDER_COLLATERAL"
                       + " WHERE ICM_PO_COL_SEQ IN (" + seq + ")";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
